<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($company_id = $route->getAdminId()))
	goToPage("/admin/amount");

$company = $db->getCell("select `name` from `companies` where `company_id` = '$company_id' limit 1");
if (!$company)
	goToPage("/admin/amount");

$errors = array();

if ($route->POST("action") == 'edit')
{
	$cc = $route->POST("cc");
	$asset = $route->POST("asset");
	$profit = $route->POST("profit");
	
	$db->sendQuery("delete from `charter_capital` where `company_id` = '$company_id'");
	$db->sendQuery("delete from `assets` where `company_id` = '$company_id'");
	$db->sendQuery("delete from `profits` where `company_id` = '$company_id'");
	
	if ($cc){
		$query = "insert into `charter_capital` values ";
		$cc_count = count($cc['date']);
		for ($i = 0; $i < $cc_count; $i++){
			$query .= "(NULL,'$company_id','".strtotime("01.01.".$cc['date'][$i])."','".str_replace(" ","",$cc['count'][$i])."')";
			if ($i < $cc_count - 1)
				$query .= ",";
		}
		if (!$db->sendQuery($query))
			$errors[] = "Ошибка обновления записи";
	}
	if ($asset){
		$query = "insert into `assets` values ";
		$asset_count = count($asset['date']);
		for ($i = 0; $i < $asset_count; $i++){
			$query .= "(NULL,'$company_id','".strtotime("01.01.".$asset['date'][$i])."','".str_replace(" ","",$asset['count'][$i])."')";
			if ($i < $asset_count - 1)
				$query .= ",";
		}
		if (!$db->sendQuery($query))
			$errors[] = "Ошибка обновления записи";
	}
	if ($profit){
		$query = "insert into `profits` values ";
		$profit_count = count($profit['date']);
		for ($i = 0; $i < $profit_count; $i++){
			$query .= "(NULL,'$company_id','".strtotime("01.01.".$profit['date'][$i])."','".str_replace(" ","",$profit['count'][$i])."')";
			if ($i < $profit_count - 1)
				$query .= ",";
		}
		if (!$db->sendQuery($query))
			$errors[] = "Ошибка обновления записи";
	}
}

loadPage("head.php");
?>
<script type='text/javascript'>
$(document).ready(function(){
	$(".del").live("click",function(){
		var parent = $(this).parents("tr");
		parent.css("background","#faa");
		var answer = confirm("Действительно удалить?");
		parent.css("background","");
		if (answer){
			parent.remove();
		}
	})
	$(".amount").live("keyup",function(e){
		if (e.keyCode < 96 || e.keyCode > 105)
			if (e.keyCode < 48 || e.keyCode > 57)
				if (e.keyCode != 8 && e.keyCode != 46)
					return false;
		var value = $(this).val().replace(/ /g,"");
		var count = Math.floor(value.length/3) - 1;
		var rest = value.length%3;
		if (rest != 0 && count > -1){
			value = value.substr(0,rest) + " " + value.substr(rest);
		}
		for (var i = 0; i < count; i++){
			var pos = (rest != 0 ? rest + 1 : 0) + (i*4) + 3;
			value = value.substr(0,pos) + " " + value.substr(pos);
		}
		$(this).val(value);
	})
	
	$("#add_cc").click(function(){
		var tr = "<tr>"+
					"<td><input type='text' class='date short' name='cc[date][]' /></td>"+
					"<td><input type='text' class='amount double_short' name='cc[count][]' /></td>"+
					"<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>"+
				"</tr>";
		$(this).parents("tr").before(tr);
	})
	$("#add_asset").click(function(){
		var tr = "<tr>"+
					"<td><input type='text' class='date short' name='asset[date][]' /></td>"+
					"<td><input type='text' class='amount double_short' name='asset[count][]' /></td>"+
					"<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>"+
				"</tr>";
		$(this).parents("tr").before(tr);
	})
	$("#add_profit").click(function(){
		var tr = "<tr>"+
					"<td><input type='text' class='date short' name='profit[date][]' /></td>"+
					"<td><input type='text' class='amount double_short' name='profit[count][]' /></td>"+
					"<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>"+
				"</tr>";
		$(this).parents("tr").before(tr);
	})
})
</script>
<h1>Редактирование сумм «<?php echo $company; ?>»</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<h3>Размер уставного капитала</h3>
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td>Дата</td>
			<td>Сумма</td>
			<td></td>
		</tr><?php
		$cc = $db->getTable("select * from `charter_capital` where `company_id` = '$company_id' order by `cc_id`");
		foreach ($cc as $c){
			echo "<tr>";
				echo "<td><input type='text' class='short' name='cc[date][]' value='".date("Y",$c['date'])."' /></td>";
				echo "<td><input type='text' class='amount double_short' name='cc[count][]' value='".makeSpaces($c['count'])."' /></td>";
				echo "<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>";
			echo "</tr>";
		}
		?>
		<tr>
			<td colspan='3' align='right'><input type='button' id='add_cc' value='Добавить' /></td>
		</tr>
	</table>
	<h3>Активы</h3>
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td>Дата</td>
			<td>Сумма</td>
			<td></td>
		</tr><?php
		$assets = $db->getTable("select * from `assets` where `company_id` = '$company_id' order by `asset_id`");
		foreach ($assets as $asset){
			echo "<tr>";
				echo "<td><input type='text' class='short' name='asset[date][]' value='".date("Y",$asset['date'])."' /></td>";
				echo "<td><input type='text' class='amount double_short' name='asset[count][]' value='".makeSpaces($asset['count'])."' /></td>";
				echo "<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>";
			echo "</tr>";
		}
		?>
		<tr>
			<td colspan='3' align='right'><input type='button' id='add_asset' value='Добавить' /></td>
		</tr>
	</table>
	<h3>Прибыль</h3>
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td>Дата</td>
			<td>Сумма</td>
			<td></td>
		</tr><?php
		$profits = $db->getTable("select * from `profits` where `company_id` = '$company_id' order by `profit_id`");
		foreach ($profits as $profit){
			echo "<tr>";
				echo "<td><input type='text' class='short' name='profit[date][]' value='".date("Y",$profit['date'])."' /></td>";
				echo "<td><input type='text' class='amount double_short' name='profit[count][]' value='".makeSpaces($profit['count'])."' /></td>";
				echo "<td style='text-align:right'><input type='button' class='del' value='Удалить' /></td>";
			echo "</tr>";
		}
		?>
		<tr>
			<td colspan='3' align='right'><input type='button' id='add_profit' value='Добавить' /></td>
		</tr>
		<tr>
			<td colspan='3' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>